/**
 * Created by panjun7@yunzongnet.com
 */
 var createActivityOtherSettingController = null;
(function(doc, win) {

    // sui 页面就绪时间
    $(doc).on("pageInit", "#createOtherSettingPage", function(e, pageId, $page,isBack) {
    	if(!createActivityOtherSettingController){
        	createActivityOtherSettingController = new CreateActivityOtherSettingController();
    	}
    	
        createActivityOtherSettingController.initPage(isBack);
    });


    //发放数量、累计领取上限、单日领取上限
    $(doc).on('click', '#createOtherSettingPage #total-deadline.switch-label', function () {
        if($(this).attr("data-disabled") == 'true'){
            return;
        }

        var _text = $(this).children("span");
        var _self = $(this);
        var bindPropName = $(this).attr("data-bindPropName");
        var buttons1 = [
            {
                text: '不限制',
                onClick: function () {
                    _text.text("不限制");
                    _self.siblings("p").hide();
                    createActivityOtherSettingController.otherSettingInfo[bindPropName]=1;
                }
            },
            {
                text: '限制',
                onClick: function () {
                    _text.text("限制");
                    _self.siblings("p").show();
                    createActivityOtherSettingController.otherSettingInfo[bindPropName]=2;
                }
            }
        ];
        var buttons2 = [
            {
                text: '取消',
            }
        ];
        var groups = [buttons1, buttons2];
        $.actions(groups);
    });

    //生效方式
    $(doc).on('click', '#createOtherSettingPage #effect-type.switch-label', function () {

        if($(this).attr("data-disabled") == 'true'){
            return;
        }


        var _text = $(this).children("span");
        var _self = $(this);
        var bindPropName = _self.attr("data-bindPropName")
        var buttons1 = [
            {
                text: '当日生效',
                onClick: function () {
                    _text.text("当日生效");
                    _self.siblings("p").hide();
                    createActivityOtherSettingController.otherSettingInfo[bindPropName]='IMMEDIATELY';
                }
            },
            {
                text: '次日生效',
                onClick: function () {
                    _text.text("次日生效");
                    _self.siblings("p").show();
                    createActivityOtherSettingController.otherSettingInfo[bindPropName]='DELAY';
                }
            }
        ];
        var buttons2 = [
            {
                text: '取消',
            }
        ];
        var groups = [buttons1, buttons2];
        $.actions(groups);
    });


    //是否可转增
    $(doc).on("click","#createOtherSettingPage #voucherDonateFlagLabelSwitch",function(){
        if($(this).attr("data-disabled") == 'true'){
            return false;
        }
    });

     $(doc).on('click', '#createOtherSettingPage .voucherLogoSelect', function () {
        var _text = $(this).children("span");
        var _self = $(this);
        var buttons1 = [
            {
                text: '相册',
                onClick: function () {

                }
            },
            {
                text: '拍照',
                onClick: function () {

                }
            }
        ];
        var buttons2 = [
            {
                text: '取消',
            }
        ];
        var groups = [buttons1, buttons2];
        $.actions(groups);
    });

    //适用门店选择
    $(doc).on("click","#createOtherSettingPage .suitShopsSelect",function(){
        createActivityOtherSettingController.saveOtherSettingInfo();
    	var otherSettingInfo =  createActivityOtherSettingController.otherSettingInfo;
    	var paramObj = {"suitShopsSelect":otherSettingInfo.suitShopsSelect,
    					"suitShops":otherSettingInfo.suitShops,
                        "activityType":otherSettingInfo.activityType};
    	CommonUtil.injectParamObject(paramObj);
    	$.router.load("suit_shops.html");
    });

    //使用说明
    $(doc).on("click","#createOtherSettingPage .voucherUseInstructions",function(){
        createActivityOtherSettingController.saveOtherSettingInfo();
        CommonUtil.injectParamObject({"voucherUseInstructions":createActivityOtherSettingController.otherSettingInfo.voucherUseInstructions});
        $.router.load("direction_for_use.html");
    });

    //活动名称最多32字
    $(document).on("input", "#createOtherSettingPage .activityName", function () {
        if($(this).val().length>32){
            $this.val($this.val().substr(0,32));
        }
    });


    $(document).on("focus",'#createOtherSettingPage .budgetValue,.userWinCount,.userWinFrequencyValue', function (e){
        if($(this).attr("data-disabled") == 'true'){
            $(this).blur();
            return false;
        }

        return true;
    });
    
    //发放数量、累计领取上限、单日领取上限不允许输入非数字
    $(document).on('keypress', '#createOtherSettingPage .budgetValue,.userWinCount,.userWinFrequencyValue', function (e) {
        log4JS.info("data-disabled"+$(this).attr("data-disabled"));
        if($(this).attr("data-disabled") == 'true'){
            $(this).blur();
            return false;
        }

        if($(this).val().length==0){
                return /^[1-9]+$/.test(String.fromCharCode(e.keyCode));
        }else{
                return /^[0-9]+$/.test(String.fromCharCode(e.keyCode));
        }
            
    });

    //发放数量、累计领取上限、单日领取上限不允许输入非数字
    $(document).on("input","#createOtherSettingPage .budgetValue,.userWinCount,.userWinFrequencyValue",function(){
        log4JS.info("data-disabled"+$(this).attr("data-disabled"));
        if($(this).attr("data-disabled") == 'true'){
            $(this).blur();
            return false;
        }

        var $this = $(this);
        if($this.val().length>9){
             $this.val($this.val().substr(0,9));
             $this.val($this.val().replace(new RegExp(/[^0-9]/g),''));
        }
    });

    //券图片
    $(document).on('click', '.voucherImg', function () {
        var buttons1 = [
            {
                text: '相册',
                onClick: function () {
                         AppUtil.call(AppUtil.APIS.SELECT_PICTURE,
                            {compress : {"sizeLimit" : "2","unit" : "M"},
                             crop:{heightRatio:1,widthRatio:1},
                             format:"JPG",
                             maxNum:1
                            },
                            function(result){
                                createActivityOtherSettingController.selectVoucherImgCallback(result);
                        });
                }
            },
            {
                text: '拍照',
                onClick: function () {
                    AppUtil.call(AppUtil.APIS.TAKE_PICTURE,
                            {compress : {"sizeLimit" : "2","unit" : "M"},
                             crop:{heightRatio:1,widthRatio:1},
                             format:"JPG",
                             maxNum:1
                            },
                            function(result){
                                createActivityOtherSettingController.selectVoucherImgCallback(result);
                    });
                }
            }
        ];
        var buttons2 = [
            {
                text: '取消',
            }
        ];
        var groups = [buttons1, buttons2];
        $.actions(groups);
    });

     //品牌logo
    $(document).on('click', '.voucherLogo .switch-label', function () {
        var buttons1 = [
            {
                text: '相册',
                onClick: function () {
                         AppUtil.call(AppUtil.APIS.SELECT_PICTURE,
                            {compress : {"sizeLimit" : "2","unit" : "M"},
                             crop:{heightRatio:1,widthRatio:1},
                             format:"JPG",
                             maxNum:1
                            },
                            function(result){
                                createActivityOtherSettingController.selectVoucherLogoCallback(result);
                        });
                }
            },
            {
                text: '拍照',
                onClick: function () {
                    AppUtil.call(AppUtil.APIS.TAKE_PICTURE,
                            {compress : {"sizeLimit" : "2","unit" : "M"},
                             crop:{heightRatio:1,widthRatio:1},
                             format:"JPG",
                             maxNum:1
                            },
                            function(result){
                                createActivityOtherSettingController.selectVoucherLogoCallback(result);
                    });
                }
            }
        ];
        var buttons2 = [
            {
                text: '取消',
            }
        ];
        var groups = [buttons1, buttons2];
        $.actions(groups);
    });
    


    //返回
    $(doc).on("click","#createOtherSettingPage .back",function(){
        var validationResult = createActivityOtherSettingController.valildateOtherSettingInfo();
        if(validationResult.success){
            CommonUtil.injectParamObject(createActivityOtherSettingController.otherSettingInfo);
            $.router.back();

            //清空自己页面的信息
            createActivityOtherSettingController.clearInto();
        }else{
            DialogUtil.dialogWarn(validationResult.errorMsgs[0]);
        }
    });
})(document, window);

